package com.rocket.excel;

import java.io.File;
import java.util.ArrayList;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.rocket.database.Missile83Sql;
import com.rocket.database.MissileJD89Sql;
import com.rocket.database.RocketBL1Sql;
import com.rocket.database.RocketHJD82Sql;
import com.rocket.database.RocketRYI6300Sql;
import com.rocket.database.RocketWR98Sql;
import com.rocket.entity.Missile83;
import com.rocket.entity.MissileJD89;
import com.rocket.entity.RocketBL1;
import com.rocket.entity.RocketHJD82;
import com.rocket.entity.RocketRYI6300;
import com.rocket.entity.RocketWR98;

public class DataBaseExcelUtil extends ExcelUtil {

	private static DataBaseExcelUtil	mInstance;

	public static synchronized DataBaseExcelUtil getInstance() {
		if (mInstance == null) {
			mInstance = new DataBaseExcelUtil();
		}
		return mInstance;
	}

	public void export(final String path) {
		new Thread() {

			public void run() {

				File file = new File(path);

				try {
					WritableWorkbook wwb = Workbook.createWorkbook(file);

					exportMissile83DataBase(wwb);
					exportMissileJD89DataBase(wwb);
					exportRocketBL1DataBase(wwb);
					exportRocketHJD82DataBase(wwb);
					exportRocketRYI6300DataBase(wwb);
					exportRocketWR98DataBase(wwb);

					wwb.write();
					wwb.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

			};
		}.start();
	}

	private void exportMissile83DataBase(WritableWorkbook wwb) {
		try {
			WritableSheet sheet = wwb.createSheet("83型炮弹", 0);

			sheet.mergeCells(0, 0, 0, 2);
			sheet.mergeCells(1, 0, 9, 0);
			sheet.mergeCells(1, 1, 9, 1);

			sheet.addCell(new Label(0, 0, "高炮发射仰角"));
			sheet.addCell(new Label(1, 0, "引信自炸时间（秒）"));
			sheet.addCell(new Label(1, 1, "炸点高度（米）/水平距离（米）"));
			sheet.addCell(new Label(0, 3, "85°"));
			sheet.addCell(new Label(0, 4, "80°"));
			sheet.addCell(new Label(0, 5, "75°"));
			sheet.addCell(new Label(0, 6, "70°"));
			sheet.addCell(new Label(0, 7, "65°"));
			sheet.addCell(new Label(0, 8, "60°"));
			sheet.addCell(new Label(0, 9, "55°"));
			sheet.addCell(new Label(0, 10, "50°"));
			sheet.addCell(new Label(0, 11, "45°"));
			sheet.addCell(new Label(1, 2, "8"));
			sheet.addCell(new Label(2, 2, "10"));
			sheet.addCell(new Label(3, 2, "12"));
			sheet.addCell(new Label(4, 2, "14"));
			sheet.addCell(new Label(5, 2, "16"));
			sheet.addCell(new Label(6, 2, "18"));
			sheet.addCell(new Label(7, 2, "20"));

			int[] angles = new int[] { 85, 80, 75, 70, 65, 60, 55, 50, 45 };
			for (int j = 0; j < angles.length; j++) {
				ArrayList<Missile83> missiles = Missile83Sql.getInstance().queryWithAngle(angles[j]);
				for (int i = 0; i < missiles.size(); i++) {
					Missile83 missile = missiles.get(i);
					sheet.addCell(new Label(i + 1, j + 3, missile.height + "/" + missile.length));
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exportMissileJD89DataBase(WritableWorkbook wwb) {
		try {
			WritableSheet sheet = wwb.createSheet("JD-89型炮弹", 1);

			sheet.mergeCells(0, 0, 0, 2);
			sheet.mergeCells(1, 0, 8, 0);
			sheet.mergeCells(1, 1, 8, 1);

			sheet.addCell(new Label(0, 0, "高炮发射仰角"));
			sheet.addCell(new Label(1, 0, "引信自炸时间（秒）"));
			sheet.addCell(new Label(1, 1, "炸点高度（米）/水平距离（米）"));
			sheet.addCell(new Label(0, 3, "85°"));
			sheet.addCell(new Label(0, 4, "80°"));
			sheet.addCell(new Label(0, 5, "75°"));
			sheet.addCell(new Label(0, 6, "70°"));
			sheet.addCell(new Label(0, 7, "65°"));
			sheet.addCell(new Label(0, 8, "60°"));
			sheet.addCell(new Label(0, 9, "55°"));
			sheet.addCell(new Label(0, 10, "50°"));
			sheet.addCell(new Label(0, 11, "45°"));
			sheet.addCell(new Label(1, 2, "8"));
			sheet.addCell(new Label(2, 2, "10"));
			sheet.addCell(new Label(3, 2, "12"));
			sheet.addCell(new Label(4, 2, "14"));
			sheet.addCell(new Label(5, 2, "16"));
			sheet.addCell(new Label(6, 2, "18"));

			int[] angles = new int[] { 85, 80, 75, 70, 65, 60, 55, 50, 45 };
			for (int j = 0; j < angles.length; j++) {
				ArrayList<MissileJD89> missiles = MissileJD89Sql.getInstance().queryWithAngle(angles[j]);
				for (int i = 0; i < missiles.size(); i++) {
					MissileJD89 missile = missiles.get(i);
					sheet.addCell(new Label(i + 1, j + 3, missile.height + "/" + missile.length));
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void exportRocketBL1DataBase(WritableWorkbook wwb) {
		try {
			ArrayList<RocketBL1> rockets = RocketBL1Sql.getInstance().query();
			WritableSheet sheet = wwb.createSheet("BL-1型火箭弹", 2);

			sheet.mergeCells(0, 0, 0, 1);
			sheet.mergeCells(1, 0, 2, 0);
			sheet.mergeCells(3, 0, 4, 0);
			sheet.mergeCells(5, 0, 6, 0);
			sheet.mergeCells(7, 0, 8, 0);

			sheet.addCell(new Label(0, 0, "发射角（度）"));
			sheet.addCell(new Label(1, 0, "撒播起点"));
			sheet.addCell(new Label(1, 1, "射高（米）"));
			sheet.addCell(new Label(2, 1, "射程（米）"));
			sheet.addCell(new Label(3, 0, "最高点"));
			sheet.addCell(new Label(3, 1, "射高（米）"));
			sheet.addCell(new Label(4, 1, "射程（米）"));
			sheet.addCell(new Label(5, 0, "撒播终点"));
			sheet.addCell(new Label(5, 1, "射高（米）"));
			sheet.addCell(new Label(6, 1, "射程（米）"));
			sheet.addCell(new Label(7, 0, "自毁点"));
			sheet.addCell(new Label(7, 1, "射高（米）"));
			sheet.addCell(new Label(8, 1, "射程（米）"));

			for (int i = 0; i < rockets.size(); i++) {
				RocketBL1 rocket = rockets.get(i);
				sheet.addCell(new Label(0, i + 2, String.valueOf(rocket.angle)));
				sheet.addCell(new Label(1, i + 2, String.valueOf(rocket.startHeight)));
				sheet.addCell(new Label(2, i + 2, String.valueOf(rocket.startLength)));
				sheet.addCell(new Label(3, i + 2, String.valueOf(rocket.topHeight)));
				sheet.addCell(new Label(4, i + 2, String.valueOf(rocket.topLength)));
				sheet.addCell(new Label(5, i + 2, String.valueOf(rocket.endHeight)));
				sheet.addCell(new Label(6, i + 2, String.valueOf(rocket.endLength)));
				sheet.addCell(new Label(7, i + 2, String.valueOf(rocket.destoryHeight)));
				sheet.addCell(new Label(8, i + 2, String.valueOf(rocket.destoryLength)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exportRocketHJD82DataBase(WritableWorkbook wwb) {
		try {
			ArrayList<RocketHJD82> rockets = RocketHJD82Sql.getInstance().query();

			WritableSheet sheet = wwb.createSheet("HJD-82型火箭弹", 3);

			sheet.mergeCells(0, 0, 0, 1);
			sheet.mergeCells(1, 0, 2, 0);
			sheet.mergeCells(3, 0, 4, 0);
			sheet.mergeCells(5, 0, 6, 0);

			sheet.addCell(new Label(0, 0, "发射角（度）"));
			sheet.addCell(new Label(1, 0, "撒播起点"));
			sheet.addCell(new Label(1, 1, "射高（米）"));
			sheet.addCell(new Label(2, 1, "射程（米）"));
			sheet.addCell(new Label(3, 0, "最高点"));
			sheet.addCell(new Label(3, 1, "射高（米）"));
			sheet.addCell(new Label(4, 1, "射程（米）"));
			sheet.addCell(new Label(5, 0, "撒播终点"));
			sheet.addCell(new Label(5, 1, "射高（米）"));
			sheet.addCell(new Label(6, 1, "射程（米）"));

			for (int i = 0; i < rockets.size(); i++) {
				RocketHJD82 rocket = rockets.get(i);
				sheet.addCell(new Label(0, i + 2, String.valueOf(rocket.angle)));
				sheet.addCell(new Label(1, i + 2, String.valueOf(rocket.startHeight)));
				sheet.addCell(new Label(2, i + 2, String.valueOf(rocket.startLength)));
				sheet.addCell(new Label(3, i + 2, String.valueOf(rocket.topHeight)));
				sheet.addCell(new Label(4, i + 2, String.valueOf(rocket.topLength)));
				sheet.addCell(new Label(5, i + 2, String.valueOf(rocket.endHeight)));
				sheet.addCell(new Label(6, i + 2, String.valueOf(rocket.endLength)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exportRocketRYI6300DataBase(WritableWorkbook wwb) {
		try {
			ArrayList<RocketRYI6300> rockets = RocketRYI6300Sql.getInstance().query();
			WritableSheet sheet = wwb.createSheet("RYI-6300型火箭弹", 4);

			sheet.mergeCells(0, 0, 0, 1);
			sheet.mergeCells(1, 0, 2, 0);
			sheet.mergeCells(3, 0, 4, 0);
			sheet.mergeCells(5, 0, 6, 0);

			sheet.addCell(new Label(0, 0, "发射角（度）"));
			sheet.addCell(new Label(1, 0, "撒播起点"));
			sheet.addCell(new Label(1, 1, "射高（米）"));
			sheet.addCell(new Label(2, 1, "射程（米）"));
			sheet.addCell(new Label(3, 0, "最高点"));
			sheet.addCell(new Label(3, 1, "射高（米）"));
			sheet.addCell(new Label(4, 1, "射程（米）"));
			sheet.addCell(new Label(5, 0, "撒播终点"));
			sheet.addCell(new Label(5, 1, "射高（米）"));
			sheet.addCell(new Label(6, 1, "射程（米）"));
			sheet.addCell(new Label(7, 0, "理论落点"));
			sheet.addCell(new Label(7, 1, "射程（米）"));

			for (int i = 0; i < rockets.size(); i++) {
				RocketRYI6300 rocket = rockets.get(i);
				sheet.addCell(new Label(0, i + 2, String.valueOf(rocket.angle)));
				sheet.addCell(new Label(1, i + 2, String.valueOf(rocket.startHeight)));
				sheet.addCell(new Label(2, i + 2, String.valueOf(rocket.startLength)));
				sheet.addCell(new Label(3, i + 2, String.valueOf(rocket.topHeight)));
				sheet.addCell(new Label(4, i + 2, String.valueOf(rocket.topLength)));
				sheet.addCell(new Label(5, i + 2, String.valueOf(rocket.endHeight)));
				sheet.addCell(new Label(6, i + 2, String.valueOf(rocket.endLength)));
				sheet.addCell(new Label(7, i + 2, String.valueOf(rocket.length)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exportRocketWR98DataBase(WritableWorkbook wwb) {
		try {
			ArrayList<RocketWR98> rockets = RocketWR98Sql.getInstance().query();
			WritableSheet sheet = wwb.createSheet("WR-98型火箭弹", 5);

			sheet.mergeCells(0, 0, 0, 1);
			sheet.mergeCells(1, 0, 2, 0);
			sheet.mergeCells(3, 0, 4, 0);
			sheet.mergeCells(5, 0, 6, 0);

			sheet.addCell(new Label(0, 0, "发射角（度）"));
			sheet.addCell(new Label(1, 0, "撒播起点"));
			sheet.addCell(new Label(1, 1, "射高（米）"));
			sheet.addCell(new Label(2, 1, "射程（米）"));
			sheet.addCell(new Label(3, 0, "最高点"));
			sheet.addCell(new Label(3, 1, "射高（米）"));
			sheet.addCell(new Label(4, 1, "射程（米）"));
			sheet.addCell(new Label(5, 0, "撒播终点"));
			sheet.addCell(new Label(5, 1, "射高（米）"));
			sheet.addCell(new Label(6, 1, "射程（米）"));

			for (int i = 0; i < rockets.size(); i++) {
				RocketWR98 rocket = rockets.get(i);
				sheet.addCell(new Label(0, i + 2, String.valueOf(rocket.angle)));
				sheet.addCell(new Label(1, i + 2, String.valueOf(rocket.startHeight)));
				sheet.addCell(new Label(2, i + 2, String.valueOf(rocket.startLength)));
				sheet.addCell(new Label(3, i + 2, String.valueOf(rocket.topHeight)));
				sheet.addCell(new Label(4, i + 2, String.valueOf(rocket.topLength)));
				sheet.addCell(new Label(5, i + 2, String.valueOf(rocket.endHeight)));
				sheet.addCell(new Label(6, i + 2, String.valueOf(rocket.endLength)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
